<?php

namespace app\api\controller;

use app\common\logic\Data;
use think\facade\Validate;
use think\facade\Db;
use app\lib\Result;
use Yurun\Util\HttpRequest;

class MpWeixin extends Base
{
  public function asyncMedia()
  {
    $rule = [
      'mw_id'  => 'require',
    ];
    $data = [
      'mw_id' => input('param.mw_id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $has_mw_id = $this->redis->get('async:media:' . $data['mw_id']);
    if (!empty($has_mw_id)) {
      return Result::Error('任务队列中，请等待执行');
    }
    $mw_id_info['mw_id'] = $data['mw_id'];
    $mw_id_info['uid'] = $this->uid;
    $mw_id_info['username'] = $this->username;
    $this->redis->set('async:media:' . $data['mw_id'], json_encode($mw_id_info));
    $this->redis->lpush('async:media:runList', $data['mw_id']);
    return Result::Success('', '同步任务已提交，请等待系统执行（此过程可能需要1-10分钟）');
  }
  /**
   * 列表
   * @Author Qianlong <87498106@qq.com>
   * @PersonSite http://www.21ds.cn/
   */
  public function list()
  {
    $rule = [
      'pageNo'  => 'number',
      'pageSize'  => 'number',
    ];
    $data = [
      'pageNo' => input('param.pageNo', 1, 'trim'),
      'title' => input('param.title', '', 'trim'),
      'pageSize' => input('param.pageSize', 15, 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    // 构建查询条件
    $where = [];

    if (!empty($data['title'])) {
      $where[] = ['title|_id|app_id', 'like', $data['title']];
    }
    $limit = ($data['pageNo'] - 1) * $data['pageSize'];
    $dataList = Db::connect('mongodb')->name('mp_weixin')->field('*')->where($where)->order('create_time', 'desc')->limit($limit, $data['pageSize'])->select()->toArray();
    $totalcount = Db::connect('mongodb')->name('mp_weixin')->where($where)->count('_id');
    foreach ($dataList as &$vo) {
      $vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
    }
    $returnData['data'] = $dataList;
    $returnData['totalCount'] = $totalcount;
    $returnData['pageNo'] = (int) $data['pageNo'];
    $returnData['pageSize'] = (int) $data['pageSize'];
    return Result::Success($returnData);
  }
  public function all_list()
  {
    $where['uid'] = $this->uid;
    $dataList = Db::connect('mongodb')->name('mp_weixin')->field('_id,title')->where($where)->order('create_time', 'desc')->select()->toArray();
    return Result::Success($dataList);
  }
  public function getMediaList()
  {
    $rule = [
      'pageNo'  => 'number',
      'pageSize'  => 'number',
    ];
    $data = [
      'pageNo' => input('param.pageNo', 1, 'trim'),
      'pageSize' => input('param.pageSize', 15, 'trim'),
      'mw_id' => input('param.mw_id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    // 构建查询条件
    $where['uid'] = $this->uid;
    $where['mw_id'] = $data['mw_id'];
    $limit = ($data['pageNo'] - 1) * $data['pageSize'];
    $dataList = Db::connect('mongodb')->name('wechat_media')->field('*')->where($where)->order('create_time', 'desc')->limit($limit, $data['pageSize'])->select()->toArray();
    $totalcount = Db::connect('mongodb')->name('wechat_media')->where($where)->count('_id');
    $allMediaId = [];
    foreach ($dataList as &$vo) {
      $allMediaId[] = $vo['_id'];
      $vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
    }
    $returnData['data'] = $dataList;
    $returnData['allId'] = $allMediaId;
    $returnData['totalCount'] = $totalcount;
    $returnData['pageNo'] = (int) $data['pageNo'];
    $returnData['pageSize'] = (int) $data['pageSize'];
    return Result::Success($returnData);
  }
  public function getMenuList()
  {
    $rule = [
      'mw_id'  => 'require',
    ];
    $data = [
      'mw_id' => input('param.mw_id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    // 构建查询条件
    $where['uid'] = $this->uid;
    $where['mw_id'] = $data['mw_id'];
    $dataList = Db::connect('mongodb')->name('mp_menu_list')->field('*')->where($where)->order('sort', 'desc')->select()->toArray();
    foreach ($dataList as &$vo) {
      $vo['key'] = $vo['_id'];
      $vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
    }
    $sdata = new Data();
    $selectResult = $sdata->listToTreeMulti($dataList, '0', '_id', 'pid', 'children');
    $selectResult = $sdata->filterEmptyKey($selectResult, 'children');
    if (empty($selectResult)) {
      $selectResult = $dataList;
    }
    $return['code'] = 200;
    $return['data'] = $selectResult;
    return json($return);
  }
  public function getKeywordList()
  {
    $rule = [
      'pageNo'  => 'number',
      'pageSize'  => 'number',
    ];
    $data = [
      'pageNo' => input('param.pageNo', 1, 'trim'),
      'title' => input('param.title', '', 'trim'),
      'mw_id' => input('param.mw_id', '', 'trim'),
      'pageSize' => input('param.pageSize', 15, 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    // 构建查询条件
    $where = [];

    if (!empty($data['title'])) {
      $where[] = ['keyword', 'like', $data['title']];
    }
    if (!empty($data['mw_id'])) {
      $where[] = ['mw_id', '=', $data['mw_id']];
    }
    $limit = ($data['pageNo'] - 1) * $data['pageSize'];
    $dataList = Db::connect('mongodb')->name('mp_weixin_keyword')->field('*')->where($where)->order('create_time', 'desc')->limit($limit, $data['pageSize'])->select()->toArray();
    $totalcount = Db::connect('mongodb')->name('mp_weixin_keyword')->where($where)->count('_id');
    foreach ($dataList as &$vo) {
      $vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
    }
    $returnData['data'] = $dataList;
    $returnData['totalCount'] = $totalcount;
    $returnData['pageNo'] = (int) $data['pageNo'];
    $returnData['pageSize'] = (int) $data['pageSize'];
    return Result::Success($returnData);
  }
  public function getSubList()
  {
    $rule = [
      'pageNo'  => 'number',
      'pageSize'  => 'number',
    ];
    $data = [
      'pageNo' => input('param.pageNo', 1, 'trim'),
      'title' => input('param.title', '', 'trim'),
      'mw_id' => input('param.mw_id', '', 'trim'),
      'pageSize' => input('param.pageSize', 15, 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    // 构建查询条件
    $where = [];

    if (!empty($data['title'])) {
      $where[] = ['reply', 'like', $data['title']];
    }
    if (!empty($data['mw_id'])) {
      $where[] = ['mw_id', '=', $data['mw_id']];
    }
    $limit = ($data['pageNo'] - 1) * $data['pageSize'];
    $dataList = Db::connect('mongodb')->name('mp_weixin_sub')->field('*')->where($where)->order('create_time', 'desc')->limit($limit, $data['pageSize'])->select()->toArray();
    $totalcount = Db::connect('mongodb')->name('mp_weixin_sub')->where($where)->count('_id');
    foreach ($dataList as &$vo) {
      $vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
    }
    $returnData['data'] = $dataList;
    $returnData['totalCount'] = $totalcount;
    $returnData['pageNo'] = (int) $data['pageNo'];
    $returnData['pageSize'] = (int) $data['pageSize'];
    return Result::Success($returnData);
  }
  public function getDgList()
  {
    $rule = [
      'pageNo'  => 'number',
      'pageSize'  => 'number',
    ];
    $data = [
      'pageNo' => input('param.pageNo', 1, 'trim'),
      'title' => input('param.title', '', 'trim'),
      'mw_id' => input('param.mw_id', '', 'trim'),
      'pageSize' => input('param.pageSize', 15, 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    // 构建查询条件
    $where = [];

    if (!empty($data['title'])) {
      $where[] = ['title|_id', 'like', $data['title']];
    }
    if (!empty($data['mw_id'])) {
      $where[] = ['mw_id', '=', $data['mw_id']];
    }
    $limit = ($data['pageNo'] - 1) * $data['pageSize'];
    $dataList = Db::connect('mongodb')->name('mp_weixin_dg')->field('*')->where($where)->order('create_time', 'desc')->limit($limit, $data['pageSize'])->select()->toArray();
    $totalcount = Db::connect('mongodb')->name('mp_weixin_dg')->where($where)->count('_id');
    foreach ($dataList as &$vo) {
      $vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
    }
    $returnData['data'] = $dataList;
    $returnData['totalCount'] = $totalcount;
    $returnData['pageNo'] = (int) $data['pageNo'];
    $returnData['pageSize'] = (int) $data['pageSize'];
    return Result::Success($returnData);
  }
  public function editKeywordInfo()
  {
    if (request()->isPost()) {
      $rule = [
        'title|标题'  => 'require|max:32',
        'keyword_arr'  => 'require',
        'type'  => 'require',
        'content_type'  => 'require',
        'mw_id'  => 'require',
        'need_sec_reply|是否需要二次回复'  => 'require',
      ];
      $data = [
        'title' => input('param.title', '', 'trim'),
        'keyword_arr' => input('param.keyword_arr/a', '', 'trim'),
        'type' => input('param.type', '', 'trim'),
        '_id' => input('param._id', '', 'trim'),
        'content_type' => input('param.content_type', '', 'trim'),
        'mw_id' => input('param.mw_id', '', 'trim'),
        'content' => input('param.content', '', 'trim'),
        'reply_url' => input('param.reply_url', '', 'trim'),
        'need_sec_reply' => input('param.need_sec_reply', '', 'trim'),
        'need_sec_msg' => input('param.need_sec_msg', '', 'trim'),
        'reply_desc' => input('param.reply_desc', '', 'trim'),
        'reply_pic' => input('param.reply_pic', '', 'trim'),
        'reply_title' => input('param.reply_title', '', 'trim'),
      ];
      $validate = Validate::rule($rule);
      $result   = $validate->check($data);
      if (!$result) {
        return Result::Error($validate->getError());
      }
      $data['keyword'] = implode(',', $data['keyword_arr']);
      if (!empty($data['_id'])) {
        $status = Db::connect('mongodb')->name('mp_weixin_keyword')->where(['_id' => $data['_id']])->update($data);
        $tag = '编辑';
      } else {
        $data['create_time'] = time();
        $data['uid'] = $this->uid;
        $data['username'] = $this->username;
        $data['_id'] = createId();
        $status = Db::connect('mongodb')->name('mp_weixin_keyword')->insert($data);
        $tag = '新增';
      }
      if ($status > 0) {
        return json(['code' => 200, 'data' => '', 'msg' => $tag . '成功']);
      } else {
        return json(['code' => -1, 'data' => '', 'msg' => $tag . '失败，请重试']);
      }
    }
  }
  public function editSubInfo()
  {
    if (request()->isPost()) {
      $rule = [
        'title|标题'  => 'require|max:32',
        'content_type'  => 'require',
        'mw_id'  => 'require',
        'type'  => 'require',
      ];
      $data = [
        'title' => input('param.title', '', 'trim'),
        '_id' => input('param._id', '', 'trim'),
        'content_type' => input('param.content_type', '', 'trim'),
        'mw_id' => input('param.mw_id', '', 'trim'),
        'type' => input('param.type', '', 'trim'),
        'content' => input('param.content', '', 'trim'),
        'reply_url' => input('param.reply_url', '', 'trim'),
        'reply_desc' => input('param.reply_desc', '', 'trim'),
        'reply_pic' => input('param.reply_pic', '', 'trim'),
        'reply_title' => input('param.reply_title', '', 'trim'),
      ];
      $validate = Validate::rule($rule);
      $result   = $validate->check($data);
      if (!$result) {
        return Result::Error($validate->getError());
      }
      if (!empty($data['_id'])) {
        $status = Db::connect('mongodb')->name('mp_weixin_sub')->where(['_id' => $data['_id']])->update($data);
        $tag = '编辑';
      } else {
        $data['create_time'] = time();
        $data['uid'] = $this->uid;
        $data['username'] = $this->username;
        $data['_id'] = createId();
        $status = Db::connect('mongodb')->name('mp_weixin_sub')->insert($data);
        $tag = '新增';
      }
      if ($status > 0) {
        return json(['code' => 200, 'data' => '', 'msg' => $tag . '成功']);
      } else {
        return json(['code' => -1, 'data' => '', 'msg' => $tag . '失败，请重试']);
      }
    }
  }
  public function editMenuInfo()
  {
    if (request()->isPost()) {
      $rule = [
        'title|菜单名称'  => 'require|max:12',
        'type'  => 'require',
        'mw_id'  => 'require',
        'sort'  => 'require',
      ];
      $data = [
        'title' => input('param.title', '', 'trim'),
        '_id' => input('param._id', '', 'trim'),
        'type' => input('param.type', '', 'trim'),
        'mw_id' => input('param.mw_id', '', 'trim'),
        'sort' => input('param.sort', '', 'trim'),
        'status' => input('param.status', '', 'trim'),
        'click_key' => input('param.click_key', '', 'trim'),
        'url' => input('param.url', '', 'trim'),
        'pid' => input('param.pid', '', 'trim'),
        'pagepath' => input('param.pagepath', '', 'trim'),
        'appid' => input('param.appid', '', 'trim'),
      ];
      $validate = Validate::rule($rule);
      $result   = $validate->check($data);
      if (!$result) {
        return Result::Error($validate->getError());
      }
      $data['status'] = intval($data['status']);
      $data['sort'] = intval($data['sort']);
      if (!empty($data['_id'])) {
        $status = Db::connect('mongodb')->name('mp_menu_list')->where(['_id' => $data['_id']])->update($data);
        $tag = '编辑';
      } else {
        $data['create_time'] = time();
        $data['uid'] = $this->uid;
        $data['username'] = $this->username;
        $data['_id'] = createId();
        $status = Db::connect('mongodb')->name('mp_menu_list')->insert($data);
        $tag = '新增';
      }
      if ($status > 0) {
        return json(['code' => 200, 'data' => '', 'msg' => $tag . '成功']);
      } else {
        return json(['code' => -1, 'data' => '', 'msg' => $tag . '失败，请重试']);
      }
    }
  }
  public function get_all_mp_menu()
  {
    $rule = [
      'mw_id'  => 'require',
    ];
    $data = [
      'mw_id' => input('param.mw_id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $menuList = Db::connect('mongodb')->name('mp_menu_list')->field('_id,title,status')->where(['pid' => '0', 'mw_id' => $data['mw_id']])->select();
    return Result::Success($menuList);
  }
  public function pushMenuWeixin()
  {
    $rule = [
      'mw_id'  => 'require',
    ];
    $data = [
      'mw_id' => input('param.mw_id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $menuList = Db::connect('mongodb')->name('mp_menu_list')->where(['status' => 1, 'mw_id' => $data['mw_id']])->select();
    $sdata = new Data();
    $selectResult = $sdata->listToTreeMulti($menuList, '0', '_id', 'pid', 'sub_button');
    $selectResult = $sdata->filterEmptyKey($selectResult, 'sub_button');
    $pushData['button'] = [];
    foreach ($selectResult as $key => $vo) {
      $item['name'] = $vo['title'];
      if (isset($vo['sub_button']) && !empty($vo['sub_button'])) {
        foreach ($vo['sub_button'] as $key2 => $vo2) {
          $item2['type'] = $vo2['type'];
          $item2['name'] = $vo2['title'];
          switch ($vo2['type']) {
            case 'click':
              $item2['key'] = $vo2['click_key'];
              break;
            case 'view':
              $item2['url'] = $vo2['url'];
              break;
            case 'miniprogram':
              $item2['url'] = $vo2['url'];
              $item2['appid'] = $vo2['appid'];
              $item2['pagepath'] = $vo2['pagepath'];
              break;
          }
          $item['sub_button'][] = $item2;
        }
      } else {
        $item['type'] = $vo['type'];
        switch ($vo['type']) {
          case 'click':
            $item['key'] = $vo['click_key'];
            break;
          case 'view':
            $item['url'] = $vo['url'];
            break;
          case 'miniprogram':
            $item['url'] = $vo['url'];
            $item['appid'] = $vo['appid'];
            $item['pagepath'] = $vo['pagepath'];
            break;
        }
      }
      $pushData['button'][] = $item;
    }
    $http = new HttpRequest;
    $access_token = getWxAccessToken($data['mw_id'], $this->uid, $http);
    $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $access_token;
    // $pushData['access_token'] = $tokenResult['data'];
    $createResponse = $http->post($url, json_encode($pushData, JSON_UNESCAPED_UNICODE),'json');
    $createResult = json_decode($createResponse->body(), true);
    if (!isset($createResult['errcode']) || $createResult['errcode'] == 0) {
      return Result::Success([],'推送成功，请稍后在公众号中查看');
    }
    return Result::Error($createResult['errmsg']);
  }
  public function deleteMenuWeixin()
  {
    $rule = [
      'mw_id'  => 'require',
    ];
    $data = [
      'mw_id' => input('param.mw_id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $http = new HttpRequest;
    $access_token = getWxAccessToken($data['mw_id'], $this->uid,$http);
    $url = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=' . $access_token;
    // $pushData['access_token'] = $tokenResult['data'];
    $createResponse = $http->get($url);
    $createResult = json_decode($createResponse->body(), true);
    if (!isset($createResult['errcode']) || $createResult['errcode'] == 0) {
      return Result::Success([],'删除成功，请稍后在公众号中查看');
    }
    return Result::Error($createResult['errmsg']);
  }
  public function editDgInfo()
  {
    if (request()->isPost()) {
      $rule = [
        'title|标题'  => 'require|max:32',
        'content_type'  => 'require',
        'mw_id'  => 'require',
        'type'  => 'require',
      ];
      $data = [
        'title' => input('param.title', '', 'trim'),
        '_id' => input('param._id', '', 'trim'),
        'mw_id' => input('param.mw_id', '', 'trim'),
        'content_type' => input('param.content_type', '', 'trim'),
        'type' => input('param.type', '', 'trim'),
        'content' => input('param.content', '', 'trim'),
        'ecapi_apkey' => input('param.ecapi_apkey', '', 'trim'),
        'ecapi_tbname' => input('param.ecapi_tbname', '', 'trim'),
        'ecapi_tbpid' => input('param.ecapi_tbpid', '', 'trim'),
        'ecapi_jd_key_id' => input('param.ecapi_jd_key_id', '', 'trim'),
      ];
      $validate = Validate::rule($rule);
      $result   = $validate->check($data);
      if (!$result) {
        return Result::Error($validate->getError());
      }
      if (!empty($data['_id'])) {
        $status = Db::connect('mongodb')->name('mp_weixin_dg')->where(['_id' => $data['_id']])->update($data);
        $tag = '编辑';
      } else {
        $data['create_time'] = time();
        $data['uid'] = $this->uid;
        $data['username'] = $this->username;
        $data['_id'] = createId();
        $status = Db::connect('mongodb')->name('mp_weixin_dg')->insert($data);
        $tag = '新增';
      }
      if ($status > 0) {
        return json(['code' => 200, 'data' => '', 'msg' => $tag . '成功']);
      } else {
        return json(['code' => -1, 'data' => '', 'msg' => $tag . '失败，请重试']);
      }
    }
  }
  public function editItemInfo()
  {
    if (request()->isPost()) {
      $rule = [
        'title|标题'  => 'require|max:32',
        'app_id'  => 'require',
        'app_secret'  => 'require',
        'token'  => 'require',
        'status'  => 'require',
        'aes'  => 'require',
        'aes_type'  => 'require',
        'server_secret'  => 'require',
      ];
      $data = [
        'title' => input('param.title', '', 'trim'),
        'app_id' => input('param.app_id', '', 'trim'),
        'app_secret' => input('param.app_secret', '', 'trim'),
        '_id' => input('param._id', '', 'trim'),
        'token' => input('param.token', '', 'trim'),
        'status' => input('param.status', '', 'trim'),
        'aes' => input('param.aes', '', 'trim'),
        'aes_type' => input('param.aes_type', '', 'trim'),
        'event_url' => input('param.event_url', '', 'trim'),
        'server_secret' => input('param.server_secret', '', 'trim'),
      ];
      $validate = Validate::rule($rule);
      $result   = $validate->check($data);
      if (!$result) {
        return Result::Error($validate->getError());
      }
      $wxCount = Db::connect('mongodb')->name('mp_weixin')->count();
      $this->security::checkVersionLimit($this->user['version'], 'weixin_num', '微信公众号数量', $wxCount);
      if (!empty($data['_id'])) {
        $status = Db::connect('mongodb')->name('mp_weixin')->where(['_id' => $data['_id']])->update($data);
        $tag = '编辑';
      } else {
        $data['create_time'] = time();
        $data['uid'] = $this->uid;
        $data['username'] = $this->username;
        $data['_id'] = createId('MW');
        $status = Db::connect('mongodb')->name('mp_weixin')->insert($data);
        $tag = '新增';
      }
      if ($status > 0) {
        return json(['code' => 200, 'data' => '', 'msg' => $tag . '成功']);
      } else {
        return json(['code' => -1, 'data' => '', 'msg' => $tag . '失败，请重试']);
      }
    }
  }
  /**
   * 删除
   * @Author Qianlong <87498106@qq.com>
   * @PersonSite http://www.21ds.cn/
   */
  public function delKeywordItem()
  {
    $rule = [
      'id'  => 'require',
    ];
    $data = [
      'id' => input('param.id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $infoData = Db::connect('mongodb')->name('mp_weixin_keyword')->where('_id', $data['id'])->find();
    if (empty($infoData)) {
      return Result::Error('内容不存在或已删除');
    }
    $status = Db::connect('mongodb')->name('mp_weixin_keyword')->where('_id', $data['id'])->delete();
    Db::connect('mongodb')->name('mp_weixin_keyword_log')->where('k_id', $data['id'])->delete();
    if ($status > 0) {
      return Result::Success('', '删除成功');
    } else {
      return Result::Error('删除失败，请稍后重试');
    }
  }
  public function delSubItem()
  {
    $rule = [
      'id'  => 'require',
    ];
    $data = [
      'id' => input('param.id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $infoData = Db::connect('mongodb')->name('mp_weixin_sub')->where('_id', $data['id'])->find();
    if (empty($infoData)) {
      return Result::Error('内容不存在或已删除');
    }
    $status = Db::connect('mongodb')->name('mp_weixin_sub')->where('_id', $data['id'])->delete();
    Db::connect('mongodb')->name('mp_weixin_sub_log')->where('k_id', $data['id'])->delete();
    if ($status > 0) {
      return Result::Success('', '删除成功');
    } else {
      return Result::Error('删除失败，请稍后重试');
    }
  }
  public function delMenuItem()
  {
    $rule = [
      'id'  => 'require',
    ];
    $data = [
      'id' => input('param.id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $infoData = Db::connect('mongodb')->name('mp_menu_list')->where('_id', $data['id'])->find();
    if (empty($infoData)) {
      return Result::Error('内容不存在或已删除');
    }
    $status = Db::connect('mongodb')->name('mp_menu_list')->where('_id', $data['id'])->delete();
    if ($status > 0) {
      return Result::Success('', '删除成功');
    } else {
      return Result::Error('删除失败，请稍后重试');
    }
  }
  public function delDgItem()
  {
    $rule = [
      'id'  => 'require',
    ];
    $data = [
      'id' => input('param.id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $infoData = Db::connect('mongodb')->name('mp_weixin_dg')->where('_id', $data['id'])->find();
    if (empty($infoData)) {
      return Result::Error('内容不存在或已删除');
    }
    $status = Db::connect('mongodb')->name('mp_weixin_dg')->where('_id', $data['id'])->delete();
    if ($status > 0) {
      return Result::Success('', '删除成功');
    } else {
      return Result::Error('删除失败，请稍后重试');
    }
  }
  public function useReply()
  {
    $rule = [
      'id'  => 'require',
    ];
    $data = [
      'id' => input('param.id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $infoData = Db::connect('mongodb')->name('mp_weixin_sub')->where('_id', $data['id'])->find();
    if (empty($infoData)) {
      return Result::Error('内容不存在或已删除');
    }
    Db::connect('mongodb')->name('mp_weixin_sub')->where('mw_id', $infoData['mw_id'])->update(['is_use' => 0]);
    Db::connect('mongodb')->name('mp_weixin_sub')->where('_id', $data['id'])->update(['is_use' => 1]);
    return Result::Success('', '启用成功');
  }
  public function useReplyDg()
  {
    $rule = [
      'id'  => 'require',
    ];
    $data = [
      'id' => input('param.id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $infoData = Db::connect('mongodb')->name('mp_weixin_dg')->where('_id', $data['id'])->find();
    if (empty($infoData)) {
      return Result::Error('内容不存在或已删除');
    }
    Db::connect('mongodb')->name('mp_weixin_dg')->where('mw_id', $infoData['mw_id'])->update(['is_use' => 0]);
    Db::connect('mongodb')->name('mp_weixin_dg')->where('_id', $data['id'])->update(['is_use' => 1]);
    return Result::Success('', '启用成功');
  }
  public function delItem()
  {
    $rule = [
      'id'  => 'require',
    ];
    $data = [
      'id' => input('param.id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $infoData = Db::connect('mongodb')->name('mp_weixin')->where('_id', $data['id'])->find();
    if (empty($infoData)) {
      return Result::Error('内容不存在或已删除');
    }
    $status = Db::connect('mongodb')->name('mp_weixin')->where('_id', $data['id'])->delete();
    Db::connect('mongodb')->name('wechat_media')->where('mw_id', $data['id'])->delete();
    Db::connect('mongodb')->name('mp_menu_list')->where('mw_id', $data['id'])->delete();
    Db::connect('mongodb')->name('mp_weixin_keyword')->where('mw_id', $data['id'])->delete();
    Db::connect('mongodb')->name('mp_weixin_sub')->where('mw_id', $data['id'])->delete();
    Db::connect('mongodb')->name('mp_weixin_dg')->where('mw_id', $data['id'])->delete();
    if ($status > 0) {
      return Result::Success('', '删除成功');
    } else {
      return Result::Error('删除失败，请稍后重试');
    }
  }
  public function delMediaItem()
  {
    $rule = [
      'id'  => 'require',
      'mw_id'  => 'require',
    ];
    $data = [
      'id' => input('param.id/a', '', 'trim'),
      'mw_id' => input('param.mw_id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $infoData = Db::connect('mongodb')->name('wechat_media')->where('_id', 'in', $data['id'])->select();
    if (empty($infoData)) {
      return Result::Error('内容不存在或已删除');
    }
    $http = new HttpRequest;
    $access_token = getWxAccessToken($data['mw_id'], $this->uid, $http);
    $url = 'https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=' . $access_token;
    $post['access_token'] = $access_token;
    foreach ($infoData as $key => $vo) {
      $post['media_id'] = $vo['media_id'];
      $http->post($url, $post, 'json');
    }
    $status = Db::connect('mongodb')->name('wechat_media')->where('_id', 'in', $data['id'])->delete();
    if ($status > 0) {
      return Result::Success('', '删除成功');
    } else {
      return Result::Error('删除失败，请稍后重试');
    }
  }
}
